﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace NRapid.Owin
{
    public struct TContentType
    {
        public TContentType(string contentString)
        {
            string[] tempArray= contentString.Split(';');
            if (tempArray.Length > 1)
            {
                this.type = tempArray[0];
                this.charset = tempArray[1];
            }
            else
            {
                this.type = tempArray[0];
                this.charset = null;
            }
        }
        public TContentType(string type, string charset)
        {
            this.type = type;
            this.charset = charset;
        }
        public string type;
        public string charset;
    }
    public class HeaderCollection
    {
        private IDictionary<string, string[]> collection = null;
        public TContentType contentType;
        private bool hasModify = false;
        public IDictionary<string, string[]> Headers
        {
            get {
                return collection;
            }
            set {
                collection = value;
            }
        }
        public bool HasModify
        {
            get
            {
                return hasModify;
            }
        }
        public HeaderCollection()
        {
            if (collection == null)
            {
                collection = new Dictionary<string, string[]>();
            }
        }
        public HeaderCollection(IDictionary<string, object> environment)
        {
            IDictionary<string, string[]> headers = (IDictionary<string, string[]>)environment["owin.RequestHeaders"];
            this.collection = headers;
            if (headers.ContainsKey("Content-Type"))
            {
                contentType = new TContentType(headers["Content-Type"][0]);
            }
        }
        public HeaderCollection(IDictionary<string, string[]> headers)
        {
            this.collection = headers;
        }

        public string[] this[string key]
        {
            get
            {
                if (collection.ContainsKey(key))
                {
                    return collection[key];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (value == null)
                {
                    if (collection.ContainsKey(key))
                    {
                        hasModify = true;
                        collection.Remove(key);
                    }
                }
                else
                {
                    hasModify = true;
                    if (collection.ContainsKey(key))
                    {
                        collection[key] = value;
                    }
                    else
                    {
                        collection.Add(key, value);
                    }
                }
            }
        }
        public void Set(string key, string value)
        {
            if (this.collection.ContainsKey(key))
            {
                collection[key] = new string[] { value};
            }
            else
            {
                collection.Add(key, new string[] { value });
            }
        }
        public int Count
        {
            get
            {
                return collection.Count;
            }
        }
    }
}